package com.csu.client.module.messageProcess;

import java.lang.reflect.InvocationTargetException;

import javax.swing.SwingUtilities;

import com.csu.client.module.network.socket.ClientChatSocket;
import com.csu.client.module.network.socket.ConnectSession;
import com.csu.client.tools.EIMClientConfig;
import com.csu.message.BaseMessage;
import com.csu.message.LoginResponseMessage;
import com.csu.message.MessageType;
import com.csu.message.OnlineMessage;
import com.daohang361.imqq.ui.BaseFrame;
import com.daohang361.imqq.ui.EIMLoginFrame;
import com.daohang361.imqq.ui.WaitLoginFrame;


public class LoginResponseMessageProcess implements IMessageProcess{

	public void processMessage(BaseMessage message) {
		LoginResponseMessage loginResponse=(LoginResponseMessage)message;

	    java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
		   	    WaitLoginFrame qqWaitLoginFrame=WaitLoginFrame.getInstance();
			    qqWaitLoginFrame.setVisible(false);
			}
	    });
	    
		int responseInfo=loginResponse.getLoginResponse();
		if(responseInfo==MessageType.LoginResponse_Faile){
			 int errorReason=loginResponse.getErrorReason();
			 final String error =EIMClientConfig.getLoginErrrorReasonByType(errorReason);
			 Runnable showOptionPane =  
			      new Runnable() { 
			        public void run() { 
			             EIMLoginFrame loginFrame=EIMLoginFrame.getInstance();
			      	     BaseFrame.centerWindow(loginFrame);
			      	    loginFrame.changTryIcon();
			      	     loginFrame.setVisible(true);
			        	 javax.swing.JOptionPane.showMessageDialog(loginFrame, error);
			        } 
			    }; 
			    
			    try {
					SwingUtilities.invokeAndWait(showOptionPane);
					ClientChatSocket.getInstance().setStart(false);
					ConnectSession.getInstance().close();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				} 
		}else{
			//发送上线消息
			//扩展:登陆时选择......
			OnlineMessage onlineMessage=new OnlineMessage(message.getDestQQ());
			ConnectSession.getInstance().sendTextMessage(onlineMessage);
		}
		
	}

}
